C Tidbits

Random bits of information I'd like to remember when writing (or reading) C programs.
Last modified

Parameter evaluation order

Arguments to a function are not evaluated in left-to-right order, as one would imagine. The order of evaluation (and the order of execution of side effects) is unspecified.

In the following code, it's possible that ++x will be evaluated before x, and the function could print 1 1:

int x = 0;
printf("%d %d", x, ++x);

Comma operator

The comma operator evaluates the first operand, discards the result, then evaluates the second operand (not the other way around). The following statements assigns 2 to x:

x = (1, 2);

It is guaranteed that all the side effects of the first expression will be completed before any side effect of the second expression is performed.

Compiler flags

Visual Studio (MSVC) flags:

sizeof

The sizeof operator does not evaluate its operand, so sizeof(*p) does not actually dereference the pointer.